package com.uenpay.oem.ui.trans.receivable;

import android.app.Activity;
import android.graphics.Bitmap;
import android.text.SpannableString;
import android.text.TextUtils;

import com.google.zxing.WriterException;
import com.qyx.android.message.type.PacketType;
import com.socks.library.KLog;
import com.uenpay.im.IMApplication;
import com.uenpay.im.configuration.APIConfiguration;
import com.uenpay.im.utilities.DensityUtils;
import com.uenpay.im.zxing.encoding.EncodingHandler;
import com.uenpay.oem.common.ACacheConstant;
import com.uenpay.util.common.ACache;
import com.uenpay.util.common.StringUtils;
import com.uenpay.util.crypt.SafeUrlCrypt;
import com.uenpay.util.imageloader.BaseImageLoaderStrategy;
import com.uenpay.util.imageloader.ImageLoaderUtil;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Author: liulou
 * Date: 2017/6/23.
 * desc:
 */

public class QRCodeCollectionPresenter implements QRCodeCollectionContract.Presenter {

    private QRCodeCollectionContract.View mView;


    public QRCodeCollectionPresenter(QRCodeCollectionContract.View view) {
        mView = view;
    }

    @Override
    public void getQrCode(final String url, final int widthAndHeight, final boolean isDefault) {
        if (isDefault && ACache.get((Activity) mView).getAsBitmap(ACacheConstant.CACHE_PAY_QRCODE) != null) {
            mView.showQrCode(ACache.get((Activity) mView).getAsBitmap(ACacheConstant.CACHE_PAY_QRCODE));
            KLog.d("show qrcode from cache");
            return;
        }
        try {
            Bitmap qrCode = EncodingHandler.createQRCode(url.replace("\n", ""), widthAndHeight);
            if (isDefault) {
                ACache.get((Activity) mView).put(ACacheConstant.CACHE_PAY_QRCODE, qrCode);
            }
            mView.showQrCode(qrCode);
        } catch (WriterException e) {
            e.printStackTrace();
        }
    }

    @Override
    public String generateUrl(String amount) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("custid", IMApplication.getCustId());
            jsonObject.put("nickname", IMApplication.config.getUserName());
            jsonObject.put("status", "1");
            jsonObject.put("controller", "Cust");
//            jsonObject.put("nickname", StringUtils.string2Unicode(IMApplication.config.getUserName()));
            KLog.d("[generateUrl] jsonObject=" + jsonObject.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        if (amount == null) amount = "";
        return APIConfiguration.getUenQrCodeUrl() + "Communal/Communal/getQrCodeInfo?data=" + SafeUrlCrypt.encodeByAes(jsonObject.toString(), SafeUrlCrypt.KEY) + "#" + amount;
    }

    @Override
    public void onStart() {

    }

    @Override
    public void onDestroy() {

    }
}
